//
//  TitleCollectionViewCell.m
//  薄荷家具
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "TitleCollectionViewCell.h"
#import "TitleTableViewCell.h"
#import "ZHLNewWorking.h"
#import "LoopsView.h"
#import "Loops.h"
#import "MJRefresh.h"
#import "Items.h"
@implementation TitleCollectionViewCell
-(void)dealloc
{
    [_tableArray release];
    [_tableView release];
    [_loopsArray release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.loopsArray = [NSMutableArray array];
        
        
        [self createTableView];
        
        [self addfooter];
        
        
    }
    return self;
}

-(void)addfooter
{
    __block TitleCollectionViewCell *cell = self;
    
    [self.tableView addFooterWithCallback:^{
        cell.currentPage += 20;
        self.isLoadingUp = YES;
        [self getModel:cell.currentPage andCell:self.flag];
    }];
   
    
}

-(void)getModel:(NSInteger)nextPage andCell:(NSString *)ID
{
    
    [ZHLNewWorking GetDataWithURL:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?set=0&ad=2&generation=4&gender=1&limit=20&offset=%ld",self.flag,self.currentPage] dic:nil sussess:^(id responseObject) {
        
        
        NSDictionary *dic = responseObject[@"data"];
        
        
        for (NSDictionary *dictionary in dic[@"items"]) {
            
            Items *item = [[Items alloc] init];
            [item setValuesForKeysWithDictionary:dictionary];
            [self.tableArray addObject:item];
        }
        
        [self.tableView footerEndRefreshing];
        [self.tableView reloadData];

        
        
    } filed:^(NSError *error) {
        
    }];
    
    
}


-(void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, kWidth, kHeight - 30 * sHeight - 118) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
   
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableView"];
    
    [self.contentView addSubview:self.tableView];
    
    [_tableView release];
}





-(void)setFlag:(NSString *)flag
{
    if (_flag != flag) {
        [_flag release];
        _flag = [flag copy];
    }
  
    
    if ([flag isEqualToString:@"108"]) {
        
       
        
            NSLog(@"%ld",self.loopsArray.count);
            LoopsView *loops = [[LoopsView alloc]initWithFrame:CGRectMake(0, 30 * sHeight,kWidth , 150 *sHeight) andArray:self.loopsArray andinterval:2];
            NSLog(@"%@",self.loopsArray);
            self.tableView.tableHeaderView = loops;
            [loops release];

        
      
        
        /*
        [ZHLNewWorking GetDataWithURL:@"http://api.liwushuo.com/v2/banners?channel=IOS" dic:nil sussess:^(id responseObject) {
            
            NSArray *array = [[responseObject objectForKey:@"data"] objectForKey:@"banners"];
            
            for (NSDictionary *dic in array) {
                Loops *loop = [[Loops alloc]init];
                [loop setValuesForKeysWithDictionary:dic];
                [self.loopsArray addObject:loop.image_url];
            }
            
            LoopsView *loops = [[LoopsView alloc]initWithFrame:CGRectMake(0, 30 * sHeight,kWidth , 150 *sHeight) andArray:self.loopsArray andinterval:2];
            NSLog(@"%@",self.loopsArray);
            self.tableView.tableHeaderView = loops;
            [loops release];
            
            
        } filed:^(NSError *error) {
            
            NSLog(@"轮播图%@",error);
        }];
      */
    }else{
        
        self.tableView.tableHeaderView = [[UIView alloc] init];
        
    }
    
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *cellidentify = @"tableViewCell";
    
    TitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
    
    if (!cell) {
        
        cell = [[TitleTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentify];
    }
    
    cell.item = self.tableArray[indexPath.row];
    
    
    return cell;
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return 150 *sHeight;
}



@end
